/*
 * Copyright (C) 2006 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.catonine.desktop;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;

import nu.zoom.catonine.desktop.rule.StyleRulesPlugIn;
import nu.zoom.catonine.prefs.Preferences;
import nu.zoom.gal.error.ErrorReporter;
import nu.zoom.swing.action.AbstractTypedAction;
import nu.zoom.swing.desktop.Workbench;
import nu.zoom.swing.desktop.common.BackendException;
import nu.zoom.swing.desktop.component.filechooser.FileChooser;
import nu.zoom.swing.desktop.component.filechooser.FileChooserFactory;
import nu.zoom.swing.desktop.component.stringmenu.StringMenu;
import nu.zoom.swing.desktop.component.stringmenu.StringMenuFactory;
import nu.zoom.swing.desktop.component.stringmenu.StringMenuItem;
import nu.zoom.swing.desktop.component.stringmenu.StringMenuListener;
import nu.zoom.swing.desktop.preferences.InvalidDataTypeException;
import nu.zoom.ui.Resources;
import nu.zoom.ui.Resources.ResourceNotFoundException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Action to open a file for tailing.
 * 
 * @author "Johan Maasing" &lt;johan@zoom.nu&gt;
 * 
 */
public class OpenFileAction extends AbstractTypedAction implements
		StringMenuListener<File> {

	private static final long serialVersionUID = 1L;
	private final Log log = LogFactory.getLog(getClass());
	private final Workbench workbench;
	private final StringMenuFactory menuFactory;
	private final Resources localizedResources;
	private final FileOpener fileOpener;
	private final ErrorReporter errorReporter;
	private final Preferences preferences;

	public OpenFileAction(final Workbench workbench,
			final Resources localizedResources,
			final FileChooserFactory chooserFactory,
			final StringMenuFactory menuFactory,
			final StyleRulesPlugIn styleRulesPlugIn,
			final FileOpener fileOpener, final ErrorReporter errorReporter,
			final Preferences preferences) throws ResourceNotFoundException {
		super();
		this.workbench = workbench;
		this.menuFactory = menuFactory;
		this.localizedResources = localizedResources;
		this.fileOpener = fileOpener;
		this.errorReporter = errorReporter;
		this.preferences = preferences;

		setName(localizedResources.getMessage("nu.zoom.catonine.open"));
		setAcceleratorKey(KeyEvent.VK_O, KeyEvent.CTRL_DOWN_MASK);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		log.trace("Obtaining a file chooser");
		FileChooser chooser;
		try {
			chooser = preferences.getFilechooser();
			log.trace("Setting the recently used file menu on the chooser");
			StringMenu<File> menu = menuFactory.getMenu(File.class,
					DesktopPlugIn.RECENT_FILES_MENU_KEY);
			chooser.setMenu(menu);
			log.trace("Requesting the file chooser to open a file");
			File file = chooser.openFile();
			log.trace("File chooser selected file: " + file);
			if (file != null) {
				fileOpener.openFile(file);
			}
		} catch (ResourceNotFoundException resourceNotFoundException) {
			errorReporter.reportError(resourceNotFoundException);
		} catch (InvalidDataTypeException exc) {
			errorReporter.reportError(exc);
		} catch (BackendException exc) {
			errorReporter.reportError(exc);
		}
	}

	@Override
	public void menuItemSelected(StringMenuItem<File> menuItem) {
		if (menuItem.getValue().exists()) {
			if (menuItem.getValue().canRead()) {
				try {
					this.fileOpener.openFile(menuItem.getValue());
					StringMenu<File> menu = menuFactory.getMenu(File.class,
							DesktopPlugIn.RECENT_FILES_MENU_KEY);
					menu.removeItem(menuItem);
					menu.addItem(menuItem);
				} catch (ResourceNotFoundException resourceNotFoundException) {
					errorReporter.reportError(resourceNotFoundException);
				}
			} else {
				try {
					String message = localizedResources.format(
							"nu.zoom.catonine.open.fail.noread",
							menuItem.getValue());
					errorReporter.reportError(message);
					workbench.getStatusbar().setMessage(message);
				} catch (ResourceNotFoundException resourceNotFoundException) {
					errorReporter.reportError(resourceNotFoundException);
				}
			}
		} else {
			try {
				String message = localizedResources.format(
						"nu.zoom.catonine.open.fail.noexist",
						menuItem.getValue());
				errorReporter.reportError(message);
				workbench.getStatusbar().setMessage(message);
			} catch (ResourceNotFoundException resourceNotFoundException) {
				errorReporter.reportError(resourceNotFoundException);
			}
		}
	}
}
